package com.borun.dictionary;

import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.borun.bean.Base;
import com.borun.dao.BaseDao;

public class BaseList extends JFrame implements MouseListener, ActionListener {

	private static final long serialVersionUID = 1L;
	private BaseDao dao = new BaseDao();
	private JTable table;
	private JScrollPane scrollPane;
	private JPopupMenu mainMenu;
	private JMenuItem addMenu, editMenu, delMenu, refreshMenu;

	public BaseList() {

		super("词库列表");

		this.setLayout(new GridLayout(1, 1));

		scrollPane = new JScrollPane();
		scrollPane.setViewportView(getTable());
		this.add(scrollPane);

		this.setSize(400, 300);
		this.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置
		this.setResizable(false); // 禁止改变窗体大小
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

//	 public static void main(String[] args) {
//	 new BaseList();
//	 }

	private JTable getTable() {
		if (table == null) { // 检查表格是否为空.
			table = new JTable() {
				private static final long serialVersionUID = 1L;

				public boolean isCellEditable(int row, int column) {
					return false; // 不可编辑
				}
			}; // 创建表格控件.
			table.setRowHeight(25); // 设置行高.
					
			table.setColumnSelectionAllowed (false);  
			table.setRowSelectionAllowed (true); 
			
			//table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			
			table.addMouseListener(this);
			table.getTableHeader().addMouseListener(this);
			
			String[] columns = { "编号", "词库名称" }; // 创建列名数组.
			// 创建表格模型.
			DefaultTableModel model = new DefaultTableModel(columns, 0);
			table.setModel(model); // 设置表格模型.
			List<Base> bases = new ArrayList<>();
			try {
				bases = dao.getBases();
			} catch (SQLException e) {
				e.printStackTrace();
			} // 利用方法传递list集合对象.
			for (Base base : bases) { // 遍历学生集合对象.
				String[] args = { String.valueOf(base.getBaseid()),
						base.getBasename() }; // 把学生信息拆分为数组,传递给args数组.
				model.addRow(args);
			}
		}
		return table;
	}

	public void Fresh(){
		table = null;
		scrollPane.setViewportView(getTable());
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == table && e.isMetaDown()) {
			mainMenu = new JPopupMenu();// 自己添加menu的Item

			addMenu = new JMenuItem("添加词库");
			addMenu.addActionListener(this);

			editMenu = new JMenuItem("修改词库");
			editMenu.addActionListener(this);

			delMenu = new JMenuItem("删除词库");
			delMenu.addActionListener(this);

			refreshMenu = new JMenuItem("刷新列表");
			refreshMenu.addActionListener(this);

			mainMenu.add(addMenu);
			mainMenu.add(editMenu);
			mainMenu.add(delMenu);
			mainMenu.add(refreshMenu);
			mainMenu.show(table, e.getX(), e.getY());
		}
		
		

		if (e.getClickCount() == 2) {
			if (table.getSelectedRow() < 0) {
				JOptionPane.showMessageDialog(null, "您没有选中任何词库！");
				return;
			}
			String baseid = String.valueOf(table.getValueAt(
					table.getSelectedRow(), 0));
			new WordList(baseid);
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if(e.getSource() == table.getTableHeader() && e.isMetaDown()){
			mainMenu = new JPopupMenu();// 自己添加menu的Item

			addMenu = new JMenuItem("添加词库");
			addMenu.addActionListener(this);

			refreshMenu = new JMenuItem("刷新列表");
			refreshMenu.addActionListener(this);

			mainMenu.add(addMenu);
			mainMenu.add(refreshMenu);
			mainMenu.show(table, e.getX(), e.getY());
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == addMenu) {
			new AddBase();
		} else if (e.getSource() == editMenu) {
			if (table.getSelectedRow() < 0) {
				JOptionPane.showMessageDialog(null, "您没有选中任何行！");
				return;
			}
			// JOptionPane.showMessageDialog(null, "您选中的编号为：" + baseid);
			String baseid = String.valueOf(table.getValueAt(
					table.getSelectedRow(), 0));
			new UpdateBase(baseid);
		} else if (e.getSource() == delMenu) {
			if (table.getSelectedRow() < 0) {
				JOptionPane.showMessageDialog(null, "您没有选中任何行！");
				return;
			}
			int baseid = Integer.valueOf(String.valueOf(table.getValueAt(
					table.getSelectedRow(), 0)));
			try {
				if (dao.deleteBase(baseid)) {
					JOptionPane.showMessageDialog(null, "删除成功！");
					Fresh();
				} else {
					JOptionPane.showMessageDialog(null, "系统异常，删除失败！");
				}
			} catch (HeadlessException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == refreshMenu) {
			Fresh();
		}
	}
}
